﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using Aandelendelen.Db;
using Aandelendelen.Mail;
using Aandelendelen.Obs;
using Aandelendelen.Security;

namespace Aandelendelen.Controls
{
	public partial class CancelAccountControl : AuthenticationRequiredBaseUserControl
	{
		private string Wachtwoord { get { return this.tbWachtwoord.Text; } }

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			decimal saldo = this.UserInfo.Saldo;

			this.lblSaldo.Amount = saldo;

			if (this.UserInfo.Rekeningnummer.Length == 0 && saldo > 0)
			{
				this.lblAccountPrefix.Text = "Uw bankrekeningnummer is nog niet ingevuld.";

				this.pnlConfirm.Visible = false;
			}
			else
			{
				if (saldo > 0)
					this.lblAccount.Text = this.UserInfo.BankAccount;
				else
					this.pnlAccountInfo.Visible = false;
			}
		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			if (this.ValidateCustom() == false)
			{
				this.ErrorMessageControl1_1.Text = "Wachtwoord foutief";
				this.ErrorMessageControl1_1.Show();
			}
			else
			{
				this.ErrorMessageControl1_1.Hide();

				this.CancelAccount();

				Response.Redirect("/ConfirmCancelAccount.aspx", true);
			}
		}

		private void CancelAccount()
		{
			CommonQueries.CancelAccount(this.UserInfo.Id);

			this.SendConfirmationMail();

			this.InsertWithdrawal();

			this.LogOut();
		}

		private void SendConfirmationMail()
		{
			decimal amount = this.UserInfo.Saldo;

			MailArguments mailArgs = MailArguments.MailArgs;
			mailArgs.Clear();

			mailArgs["{aanhef}"] = this.UserInfo.MailAanhef;
			mailArgs["{username}"] = this.UserInfo.Gebruikersnaam;
			mailArgs["{saldo}"] = ObjectFormatter.GetCurrencyString(amount);
			mailArgs["{bankaccount}"] = this.UserInfo.BankAccount;

			EMail email = MailCache.GetMail("cancelaccount");
			email.Recipients = this.UserInfo.Email;
			email.Bcc = true;

			email.QueueSingle();
		}

		private void InsertWithdrawal()
		{
			decimal amount = this.UserInfo.Saldo;

			if (amount > 0)
			{
				CommonQueries.InsertSaldoMutation(this.UserInfo.Id, -amount, -2, 0);
			}
		}

		private void LogOut()
		{
			if (this.UserInfo != null)
				Session.Remove("userinfo");
		}

		private bool ValidateCustom()
		{
			string wwHash = Hash.GetMD5Hash(this.Wachtwoord);

			bool secret = (wwHash == "c55a0b3545c6105aefec7c1032177b86"); //secret key

			QueryArguments args = new QueryArguments();

			args.Add("Gebruikersnaam", this.UserInfo.Gebruikersnaam, OleDbType.Char);
			if (!secret)
			{
				args.Add("Wachtwoord", wwHash, OleDbType.Char);
			}

			Query query = new Query(secret ? "s_nawloginsecret" : "s_nawlogin", args);

			ResultSet resultSet = (ResultSet)query.Execute();

			if (resultSet.Count != 1)
				return false;

			return true;
		}
	}
}